﻿using Battle.Engine.InputData;
using ProtoBuf.Message;
using System;
using System.Collections.Generic;
using UnityEngine;
using Dict.Blo;
using Dict.DataModel;

namespace UI
{
    /// <summary>
    /// 本地或网络玩家主要信息
    /// </summary>
    public class UIInstPlayerData
    {
        #region 数据
        /// <summary>
        /// guid,data
        /// </summary>
        private Dictionary<string, UIEquipment> instEquips;//实例装备
        /// <summary>
        /// guid,data
        /// </summary>
        private Dictionary<string, UITreasure> instTreasures;//实例宝物
        /// <summary>
        /// guid,data
        /// </summary>
        private Dictionary<string, UIHero> instHeros;//实例武者
       /// <summary>
       /// guid.data
       /// </summary>
        private Dictionary<string, UIPet> instPets; //实例宠物
        /// <summary>
        /// pos,data
        /// </summary>
        private Dictionary<int, UIHeroFormation> instBattleCamps;//实例阵型

        private Dictionary<int, UIHelpForamation> instHelpCamps;//助阵阵形

        /// <summary>
        /// id,data
        /// </summary>
        private Dictionary<string, TableHero> allHeros = new Dictionary<string, TableHero>();  //所有武者

        public Dictionary<string, UIHero> InstHeros
        {
            get
            {
                return instHeros;
            }
        }

        public Dictionary<string, UIPet> InstPets
        {
            get
            {
                return instPets;
            }
        }

        public List<FriendBuffer> friendBuffers = new List<FriendBuffer>();
        #endregion  数据

        public void ResetAllFormationAttribute()
        {
            friendBuffers.Clear();
            foreach (var cam in instBattleCamps)
            {
                if (cam.Value.hero != null)
                {
                    cam.Value.hero.ResetAttribute();
                    friendBuffers.AddRange(cam.Value.hero.BufferForOther);
                }
            }
            UpdateFormationLuckData();
            foreach (var cam in instBattleCamps)
            {
                if (cam.Value.hero != null)
                {
                    cam.Value.ResetAttribute(friendBuffers);
                }
            }
        }
        public void Clear()
        {
            if(instEquips != null)
            {
                instEquips.Clear();
            }
            instEquips = null;

            if (instTreasures != null)
            {
                instTreasures.Clear();
            }
            instTreasures = null;

            if (instHeros != null)
            {
                instHeros.Clear();
            }
            instHeros = null;

            if (instBattleCamps != null)
            {
                instBattleCamps.Clear();
            }
            instBattleCamps = null;


            if (instHelpCamps != null)
            {
                instHelpCamps.Clear();
            }
            instHelpCamps = null;

            friendBuffers.Clear();
        }

        #region 初始化
        public void InitData(List<ProtoBuf.Message.Equipment> equipmentList, List<ProtoBuf.Message.Treasure> treasureList, List<ProtoBuf.Message.Warrior> warriorList, List<ProtoBuf.Message.FormationItem> formationList,
          List<ProtoBuf.Message.FormationThing> helpList,List<ProtoBuf.Message.Pet> petList)
        {
            //userData = new UserData();
            //userData.InitData(player);

            instEquips = new Dictionary<string, UIEquipment>();
            instTreasures = new Dictionary<string, UITreasure>();
            instHeros = new Dictionary<string, UIHero>();
            instPets = new Dictionary<string, UIPet>();
            instBattleCamps = new Dictionary<int, UIHeroFormation>();
            instBattleCamps[1] = new UIHeroFormation(1, 1, this);
            instBattleCamps[2] = new UIHeroFormation(2, 2, this);
            instBattleCamps[3] = new UIHeroFormation(3, 3, this);
            instBattleCamps[4] = new UIHeroFormation(4, 4, this);


            AddHelpHero(helpList);
            //UnityEngine.Debug.Log("playerData.helpList:" + helpList.Count);

            //UnityEngine.Debug.Log("playerData.player:");
            AddEquipment(equipmentList);
            //UnityEngine.Debug.Log("playerData.equipmentList:" + equipmentList.Count);
            AddTreasure(treasureList);
            //UnityEngine.Debug.Log("playerData.treasureList:" + treasureList.Count);
            AddHero(warriorList);

            AddPet(petList);
            //UnityEngine.Debug.Log("playerData.warriorList:" + warriorList.Count);
            InitBattleCamp(formationList);
            ResetAllFormationAttribute();
            //UnityEngine.Debug.Log("playerData.formationList:" + formationList.Count);


        }

        #endregion

        #region 查
        public UIEquipment GetUIEquipment(string guid)
        {
            if (!instEquips.ContainsKey(guid))
            {
                return null;
            }
            return instEquips[guid];
        }
        public List<UIEquipment> GetUIEquipments(string id)
        {
            List<UIEquipment> list = new List<UIEquipment>();
            foreach(var e in instEquips)
            {
                if (e.Value.equipId == id)
                    list.Add(e.Value);
            }
            return list;
        }
        /// <summary>
        /// 根据种类 type 1，2，3,4；0为所有种类
        /// </summary>
        public List<UIEquipment> GetUIEquipmentsByType(int type)
        {
            List<UIEquipment> list = new List<UIEquipment>();
            foreach (var item in instEquips)
            {
                if (type == 0)
                {
                    list.Add(item.Value);
                }
                else if (item.Value.equipData.type == type)
                {
                    list.Add(item.Value);
                }
            }
            return list;
        }



        public UITreasure GetUITreasure(string guid)
        {
            if (!instTreasures.ContainsKey(guid))
            {
                return null;
            }
            return instTreasures[guid];
        }
        public List<UITreasure> GetUITreasures(string id)
        {
            List<UITreasure> list = new List<UITreasure>();
            foreach (var item in instTreasures)
            {
                if (item.Value.treasureId == id)
                    list.Add(item.Value);
            }

            return list;
        }
        /// <summary>
        /// 根据种类 type 5,6；0为所有种类（由于要和表对应，0,5,6会改为2,1,0）
        /// </summary>
        public List<UITreasure> GetUITreasureByType(int type, bool withExpCard = true)
        {
            List<UITreasure> list = new List<UITreasure>();
            if (type == 5)
                type = 1;//戒指
            else if (type == 6)
                type = 0;//项链
            else if (type == 0)
                type = 2;//全部
            else
                return list;

            foreach (var item in instTreasures)
            {
                if (type == 2)
                {
                    if (item.Value.treasureData.type == -1 && !withExpCard)
                        continue;
                    list.Add(item.Value);
                }
                else if (item.Value.treasureData.type == type)
                {
                    list.Add(item.Value);
                }
            }
            return list;
        }

        public List<UIEquipment> GetEquipListForSell()
        {
            List<UIEquipment> list = new List<UIEquipment>();

            foreach (var item in instEquips)
            {
                if (item.Value.owner == null && item.Value.equipData.quality <= (int)QualityValue.PURPLE)
                {
                    list.Add(item.Value);
                }
            }

            list.Sort(delegate (UIEquipment a, UIEquipment b)
            {
                //品质低的优先
                if (a.equipData.quality > b.equipData.quality)
                {
                    return 1;
                }
                else if (a.equipData.quality < b.equipData.quality)
                {
                    return -1;
                }
                else
                {
                    if (a.level > b.level)
                        return 1;
                    else if (a.level < b.level)
                        return -1;
                }

                //id相同的在一起
                return a.equipId.CompareTo(b.equipId);
            });

            return list;
        }

        public List<UITreasure> GetTreasureListForSell()
        {
            List<UITreasure> list = new List<UITreasure>();

            foreach (var item in instTreasures)
            {
                if (item.Value.owner == null && item.Value.treasureData.quality <= (int)QualityValue.PURPLE)
                {
                    list.Add(item.Value);
                }
            }
            list.Sort(delegate (UITreasure a, UITreasure b)
            {
                //品质高的优先
                if (a.treasureData.quality > b.treasureData.quality)
                {
                    return 1;
                }
                else if (a.treasureData.quality < b.treasureData.quality)
                {
                    return -1;
                }
                else
                {
                    if (a.strengthLevel > b.strengthLevel)
                        return 1;
                    else if (a.strengthLevel < b.strengthLevel)
                        return -1;
                }

                //id相同的在一起
                return a.treasureId.CompareTo(b.treasureId);

            });
            return list;
        }

        public int GetEquipTreasureTotalCount()
        {
            return instEquips.Count + instTreasures.Count;
        }

        /// <summary>
        /// 获取武者
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public List<UIHero> GetUIHeros(string id)
        {
            List<UIHero> heros = new List<UIHero>();
            foreach (var it in instHeros)
            {
                if (it.Value.heroId == id)
                {
                    heros.Add(it.Value);
                }
            }

            return heros;
        }
        public UIHero GetUIHero(string guid)
        {
            if (instHeros == null ||!instHeros.ContainsKey(guid))
            {
                return null;
            }
            return instHeros[guid];
        }


        /// <summary>
        /// 获取宠物
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public List<UIPet> GetUIPets(string id)
        {
            List<UIPet> pets = new List<UIPet>();
            foreach (var it in instPets)
            {
                if (it.Value.petId == id)
                {
                    pets.Add(it.Value);
                }
            }

            return pets;
        }
        public UIPet GetUIPet(string guid)
        {
            if (instPets == null || !instPets.ContainsKey(guid))
            {
                return null;
            }
            return instPets[guid];
        }
        /// <summary>
        /// 获取所有的Hero
        /// </summary>
        /// <returns></returns>
        public List<TableHero> GetAllHeros()
        {
            List<TableHero> list = new List<TableHero>();
            foreach(var item in Dict.DictDataManager.Instance.tableHeroDao.CacheList)
            {
                if (item.id.StartsWith(Dict.Config.TableConst.war) && item.heroClass == (int)HeroClass.Hero)
                {
                    if (string.IsNullOrEmpty(item.iconName))
                    {
                        Debug.LogWarning("图标资源为空：" + item.id);
                        continue;
                    }
                    allHeros[item.id] = item;
                    list.Add(item);
                }
            }
            list.Sort(delegate (TableHero a, TableHero b)
            {
                //品质高的优先
                if (a.quality > b.quality)
                    return -1;
                else if (a.quality < b.quality)
                    return 1;
                else
                    return 0;
                //id相同的在一起
                //return a.id.CompareTo(b.id);
            });
            return list;
        }

        public TableHero GetUIHeroById(string id)
        {
            if(allHeros.ContainsKey(id))
                return allHeros[id];
            else
            {
                throw new System.NullReferenceException ("heroId:" + id);
            }
        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="pos"></param>
        /// <returns></returns>
        public List<UIHero> GetAvailableHerosForFormation(int pos)
        {
            Dictionary<string, string> posIds = new Dictionary<string, string>();
            foreach (var item in instBattleCamps)
            {
                if (item.Value.hero != null && item.Value.campPos != pos)
                {
                    posIds[item.Value.hero.heroId] = item.Value.hero.heroId;
                }
            }

            List<UIHero> list = new List<UIHero>();
            foreach (var item in instHeros)
            {
                //排除已上阵的/助阵的
                if (item.Value.owner != null || item.Value.Help())
                {
                    continue;
                }

                //不能上阵容中已有的角色
                if (posIds.ContainsKey(item.Value.heroId))
                {
                    continue;
                }

                //排除经验宝宝
                if (item.Value.heroData.heroClass == (int)HeroClass.Exp_Eater)
                    continue;

                //排除绿卡
                if (item.Value.heroData.quality <= (int)QualityValue.GREEN)
                    continue;

                list.Add(item.Value);
            }

            list.Sort(delegate (UIHero a, UIHero b)
            {
                //品质高的优先
                if (a.heroData.quality > b.heroData.quality)
                {
                    return -1;
                }
                else if (a.heroData.quality < b.heroData.quality)
                {
                    return 1;
                }
                else
                {
                    if (a.level > b.level)
                        return -1;
                    else if (a.level < b.level)
                        return 1;
                    else
                    {
                        if (a.breachlevel > b.breachlevel)
                            return -1;
                        else if (a.breachlevel < b.breachlevel)
                            return 1;
                    }
                }

                //id相同的在一起
                return a.heroId.CompareTo(b.heroId);
            });

            return list;
        }

        public List<UIPet> GetAvailablePetsForFormation(int pos)
        {
            Dictionary<string, string> posIds = new Dictionary<string, string>();
            foreach (var item in instBattleCamps)
            {
                if (item.Value.pet != null && item.Value.campPos != pos)
                {
                    posIds[item.Value.pet.petId] = item.Value.pet.petId;
                }
            }

            List<UIPet> list = new List<UIPet>();
            foreach (var item in InstPets)
            {
                ////排除已上阵的/助阵的
                //if (item.Value.owner != null || item.Value.intima)
                //{
                //    continue;
                //}

                //不能上阵容中已有的角色
                if (posIds.ContainsKey(item.Value.petId))
                {
                    continue;
                }

                list.Add(item.Value);
            }

            list.Sort(delegate (UIPet a, UIPet b)
            {
                //品质高的优先
                if (a.petData.quality > b.petData.quality)
                {
                    return -1;
                }
                else if (a.petData.quality < b.petData.quality)
                {
                    return 1;
                }
                else
                {
                    if (a.level > b.level)
                        return -1;
                    else if (a.level < b.level)
                        return 1;
                    else
                    {
                        if (a.breachlevel > b.breachlevel)
                            return -1;
                        else if (a.breachlevel < b.breachlevel)
                            return 1;
                    }
                }

                //id相同的在一起
                return a.petId.CompareTo(b.petId);
            });

            return list;
        }
        /// <summary>
        /// 获取阵型信息
        /// </summary>
        /// <param name="pos"></param>
        /// <returns></returns>
        public UIHeroFormation GetBattleCamp(int pos)
        {
            return instBattleCamps[pos];
        }

        /// <summary>
        /// 阵型是否有空位
        /// </summary>
        /// <returns></returns>
        public bool IsHaveEmptyBattleCamp()
        {
            foreach (var camp in instBattleCamps)
            {
                if (camp.Value.IsUnlocked && camp.Value.hero == null)
                {
                    return true;
                }
            }
            return false;
        }

        public Dictionary<int, UIHeroFormation> BattleCamps
        {
            get
            {
                return instBattleCamps;
            }
        }

        public bool IsHaveEmptyHelpBattleCamp()
        {
            int slotNum = 0;
            if (CommonMethod.GetModuleOpen("array_help_pos1"))
            {
                slotNum++;
            }
            if (CommonMethod.GetModuleOpen("array_help_pos2"))
            {
                slotNum++;
            }
            if (CommonMethod.GetModuleOpen("array_help_pos3"))
            {
                slotNum++;
            }
            if (CommonMethod.GetModuleOpen("array_help_pos4"))
            {
                slotNum++;
            }
            if (CommonMethod.GetModuleOpen("array_help_pos5"))
            {
                slotNum++;
            }
            if (CommonMethod.GetModuleOpen("array_help_pos6"))
            {
                slotNum++;
            }
            if (instHelpCamps.Count < slotNum)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public Dictionary<int, UIHelpForamation> GetHelpCamps()
        {
             return instHelpCamps;
        }

        public void ChangeHelpCamp(int pos,UIHelpForamation helpForm)
        {
            instHelpCamps[pos] = helpForm;
            ResetAllFormationAttribute();
            StaticData.funcNavKit.OnHelpFormationChange();
        }

        public void RemoveHelpCamp(int pos)
        {
            instHelpCamps.Remove(pos);
            ResetAllFormationAttribute();
            StaticData.funcNavKit.OnHelpFormationChange();
        }

        public void ChangeMainHero(ProtoBuf.Message.Warrior data)
        {
            if(instHeros.ContainsKey(data.guid))
            {
                UIHero hero = new UIHero(data.id);
                hero.InitData(data);
                instHeros[data.guid] = hero;
                hero.ResetAttribute();
                ResetAllFormationAttribute();
            }
            else
            {
                Debug.LogError("Main hero guid " + data.guid + " not exist！ ");
            }
        }


        /// <summary>
        /// 英雄是否在阵容中
        /// </summary>
        /// <param name="heroId"></param>
        /// <returns></returns>
        public bool IsHeroInFomation(string heroId)
        {
            foreach (var hf in BattleCamps)
            {
                if (hf.Value.hero != null && hf.Value.hero.heroId == heroId)
                {
                    return true;
                }
            }
            return false;
        }

        /// <summary>
        /// 指定的UIhero是否在上阵阵容中
        /// </summary>
        /// <param name="hero"></param>
        /// <returns></returns>
        public bool IsUIHeroInFormation(UIHero hero)
        {
            foreach (var hf in BattleCamps)
            {
                if (hf.Value.hero != null && hf.Value.hero.guid == hero.guid)
                {
                    return true;
                }
            }
            return false;
        }

        /// <summary>
        /// 英雄在助阵阵容中
        /// </summary>
        /// <param name="heroId"></param>
        /// <returns></returns>
        public bool IsHeroInHelpFomation(string guid)
        {
            foreach (var hh in instHelpCamps)
            {
                if (hh.Value.guid == guid)
                {
                    return true;
                }
            }

            return false;
        }


        #endregion

        public void InitBattleCamp(List<ProtoBuf.Message.FormationItem> formationItemDatas,bool isChange = false)
        {
            foreach (var data in formationItemDatas)
            {
                instBattleCamps[data.formatPos].InitData(data, isChange, false);
            }
            StaticData.funcNavKit.OnFormationChange();
        }

        public void FixHeroOwners()
        {
            foreach(var formation in instBattleCamps)
            {
                if(formation.Value.hero != null)
                    formation.Value.hero.owner = formation.Value;
            }
        }

        #region 增

        public void AddEquipment(List<Equipment> equipments)
        {
            foreach (var data in equipments)
            {
                AddEquipment(data);
            }
        }
        public void AddEquipment(Equipment data)
        {
            UIEquipment equip = new UIEquipment(data.equipId, this);
            equip.InitData(data);
            instEquips[data.guid] = equip;
        }
        public void AddTreasure(List<Treasure> treasures)
        {
            foreach (var data in treasures)
            {
                AddTreasure(data);
            }
        }
        public void AddTreasure(Treasure data)
        {
            UITreasure treasure = new UITreasure(data.treasureId, this);
            treasure.InitData(data);
            instTreasures[data.guid] = (treasure);
        }

        public void AddHero(List<Warrior> warriors)
        {
            foreach (var data in warriors)
            {
                UIHero hero = new UIHero(data.id);
                hero.InitData(data);
                if(hero.heroData.heroClass == (int)HeroClass.Main_Actor_Man)
                {
                    StaticData.playerData.isPlayerMan = true;
                }
                else if(hero.heroData.heroClass == (int)HeroClass.Main_Actor_Woman)
                {
                    StaticData.playerData.isPlayerMan = false;
                }
                instHeros[data.guid] = (hero);
            }
        }

        public void AddPet(List<Pet> petList)
        {
            foreach (var data in petList)
            {
                UIPet pet = new UIPet(data.id);
                pet.InitData(data);
                instPets[data.guid] = (pet);
            }
        }

        public void AddPet(Pet data)
        {
            UIPet pet = new UIPet(data.id);
            pet.InitData(data);
            instPets[data.guid] = (pet);
        }


        public void AddHelpHero(List<FormationThing> helpList)
        {
            // 填充助阵信息
            instHelpCamps = new Dictionary<int, UIHelpForamation>();
            foreach (var it in helpList)
            {
                instHelpCamps[it.pos] = new UIHelpForamation(it.pos, it.guid);
            }
            StaticData.funcNavKit.OnHelpFormationChange();
        }

        public void AddHero(Warrior data)
        {
            UIHero hero = new UIHero(data.id);
            hero.InitData(data);
            instHeros[data.guid] = (hero);
        }
        
        #endregion

        #region 删
        public void DeleteEquipment(List<string> equipments)
        {
            foreach (var data in equipments)
            {
                DeleteEquipment(data);
            }
        }
        public void DeleteTreasure(List<string> treasures)
        {
            foreach (var data in treasures)
            {
                DeleteTreasure(data);
            }
        }

        public void DeleteHero(List<string> warriors)
        {
            foreach (var data in warriors)
            {
                DeleteHero(data);
            }
        }

        public void DeletePet(List<string> pets)
        {
            foreach(var data in pets)
            {
                DeletePet(data);
            }
        }

        public void DeleteEquipment(string guid)
        {
            instEquips.Remove(guid);
        }
        public void DeleteTreasure(string guid)
        {
            instTreasures.Remove(guid);
        }

        public void DeletePet(string guid)
        {
            InstPets.Remove(guid);
        }
        public void DeleteHero(string guid)
        {
            instHeros.Remove(guid);
        }
        #endregion

        #region 改
        public void UpdateEquipment(Equipment data)
        {
            if (instEquips.ContainsKey(data.guid))
            {
                instEquips[data.guid].InitData(data);
                if (instEquips[data.guid].owner != null)
                {
                    instEquips[data.guid].owner.ResetAttribute(friendBuffers);
                }
            }
            else
            {
                AddEquipment(data);
            }
        }

        public void UpdateBattleFormation(List<ProtoBuf.Message.FormationItem> ls)
        {
            foreach (var item in ls)
            {
                foreach (var pos in instBattleCamps)
                {
                    if (pos.Value.campPos == item.formatPos)
                    {
                        pos.Value.battlePos = item.battlePos;
                        break;
                    }
                }
            }
            StaticData.funcNavKit.OnFormationChange();
        }

        public void UpdateTreasure(Treasure data)
        {
            if (instTreasures.ContainsKey(data.guid))
            {
                instTreasures[data.guid].InitData(data);
                if (instTreasures[data.guid].owner != null)
                {
                    instTreasures[data.guid].owner.ResetAttribute(friendBuffers);
                }
            }
            else
            {
                AddTreasure(data);
            }
        }
        public void UpdateHero(Warrior data)
        {
            if (instHeros.ContainsKey(data.guid))
            {
                instHeros[data.guid].InitData(data);
                if (instHeros[data.guid].owner != null)
                    ResetAllFormationAttribute();
            }
            else
            {
                AddHero(data);
            }
        }

        public void UpdatePet(Pet data)
        {
            if (instPets.ContainsKey(data.guid))
            {
                instPets[data.guid].InitData(data);
                //if (instPets[data.guid].owner != null)
                //{
                //    instPets[data.guid].owner.ResetAttribute(friendBuffers);
                //}
            }
            else
            {
                AddPet(data);
            }
        }
        /// <summary>
        /// 刷新缘分数据
        /// </summary>
        public void UpdateFormationLuckData()
        {
            foreach (var hf in BattleCamps)
            {
                if (hf.Value.hero != null)
                {
                    hf.Value.UpdateLuck();
                }
            }
        }
        #endregion


        #region Tools

        public int GetFightPower()
        {
            double fighterPower = 0;
            foreach (var heroFormation in instBattleCamps)
            {
                fighterPower += heroFormation.Value.GetFightePower();
            }
            return (int)System.Math.Round(fighterPower);
        }

        public int GetPartnerMinLevel()
        {
            if (instHelpCamps.Count < 6)
                return 0;

            int minHeroLevel = 0;
            foreach (var item in instHelpCamps)
            {
                UIHero hero = GetUIHero(item.Value.guid);
                if (minHeroLevel == 0 || hero.level < minHeroLevel)
                {
                    minHeroLevel = hero.level;
                }
            }
            return minHeroLevel;
        }
        public Color GetQualityColor(int quality)
        {
            switch ((QualityValue)quality)
            {
                case QualityValue.WHITE:
                    return new Color((float)0xf2 / 255, (float)0xf2 / 255, (float)0xf2 / 255, 1);
                case QualityValue.GREEN:
                    return new Color(0, 1, (float)0x90 / 255, 1);
                case QualityValue.BLUE:
                    return new Color((float)0x2b / 255, (float)0x69 / 255, (float)0xda / 255, 1);
                case QualityValue.PURPLE:
                    return new Color((float)0x75 / 255, (float)0x2b / 255, (float)0xda / 255, 1);
                case QualityValue.YELLOW:
                    return new Color((float)0xc1 / 255, (float)0x44 / 255, 0, 1);
                case QualityValue.RED:
                    return new Color((float)0xc8 / 255, (float)0x23 / 255, (float)0x23 / 255, 1);
            }
            return new Color(1, 1, 1, 1);
        }

        /// <summary>
        /// 装备分解自动选择
        /// </summary>
        /// <returns></returns>
        public List<string> EquipBreakupAutoSelect(int num)
        {
            List<string> list = new List<string>();
            List<UIEquipment> equips = new List<UIEquipment>();
            foreach (var equip in instEquips)
            {
                // 已装备的不能分解
                if (equip.Value.owner != null)
                {
                    continue;
                }


                // 只选绿色和蓝色
                if (equip.Value.equipData.quality == (int)QualityValue.GREEN || equip.Value.equipData.quality == (int)QualityValue.BLUE)
                {
                    equips.Add(equip.Value);
                }
            }
            equips.Sort(delegate (UIEquipment x, UIEquipment y) {
                return x.equipData.quality.CompareTo(y.equipData.quality);
            });
            foreach (var equip in equips)
            {
                list.Add(equip.guid);
                if (list.Count >= num)
                {
                    break;
                }
            }
            return list;
        }

        /// <summary>
        /// 英雄分解自动选择
        /// </summary>
        /// <returns></returns>
        public List<string> HeroBreakupAutoSelect(int num)
        {
            List<string> list = new List<string>();
            List<UIHero> heros = new List<UIHero>();
            foreach (var item in InstHeros)
            {
                // 只有普通的英雄卡可以分解
                if (item.Value.heroData.heroClass != (int)HeroClass.Hero)
                {
                    continue;
                }

                if (IsUIHeroInFormation(item.Value))
                {
                    continue;
                }

                if (IsHeroInHelpFomation(item.Value.guid))
                {
                    continue;
                }

                // 自动添加只添加绿色和蓝色
                if (item.Value.heroData.quality == (int)QualityValue.BLUE || item.Value.heroData.quality == (int)QualityValue.GREEN)
                {
                    heros.Add(item.Value);
                }
            }

            heros.Sort(delegate (UIHero x, UIHero y) {
                return x.heroData.quality.CompareTo(y.heroData.quality);
            });
            foreach (var hero in heros)
            {
                list.Add(hero.guid);
                if (list.Count >= num)
                {
                    break;
                }
            }
            return list;
        }


        /// <summary>
        /// 获取英雄升级可吃卡牌list，并排序,forAuto=true 时，为自动选择：只能自动选蓝卡以下的
        /// </summary>
        /// <param name="forAuto"></param>
        /// <returns></returns>
        public List<string> GetHeroUpgradeAvailableListInOrder(bool forAuto = false)
        {
            List<string> list = new List<string>();
            foreach (var item in instHeros)
            {
                if(item.Value.heroData.heroClass != (int)HeroClass.Main_Actor_Man && item.Value.heroData.heroClass != (int)HeroClass.Main_Actor_Woman)
                {
                    if (forAuto)
                    {
                        //TODO!!!! 经验卡牌特殊处理，且优先级低于白绿卡
                        if (item.Value.owner == null && !item.Value.Help() && (item.Value.heroData.quality < (int)QualityValue.BLUE || item.Value.heroData.heroClass == (int)HeroClass.Exp_Eater))
                        {
                            list.Add(item.Value.guid);
                        }
                    }
                    else
                    {
                        if (item.Value.owner == null && !item.Value.Help())
                        {
                            list.Add(item.Value.guid);
                        }
                    }
                } 
            }

            //排序
            list.Sort(delegate (string a0, string b0)
            {
                UIHero a = instHeros[a0];
                UIHero b = instHeros[b0];

                //经验卡优先
                if (a.heroData.heroClass == (int)HeroClass.Exp_Eater && b.heroData.heroClass != (int)HeroClass.Exp_Eater)
                    return -1;
                else if (b.heroData.heroClass == (int)HeroClass.Exp_Eater && a.heroData.heroClass != (int)HeroClass.Exp_Eater)
                    return 1;

                if (a.heroData.quality > b.heroData.quality)
                {
                    return 1;
                }
                else if (a.heroData.quality < b.heroData.quality)
                {
                    return -1;
                }
                else
                {
                    if (a.level > b.level)
                        return 1;
                    else if (a.level < b.level)
                        return -1;
                    else
                    {
                        if (a.breachlevel > b.breachlevel)
                            return 1;
                        else if (a.breachlevel < b.breachlevel)
                            return -1;
                        else
                        {
                            return a.heroId.CompareTo(b.heroId);
                        }
                    }
                }
            });

            return list;
        }
        public List<UIPet> GetPetistForBagInOrder()
        {
            List<UIPet> list = new List<UIPet>();
            foreach (var item in instPets)
            {
                list.Add(item.Value);
            }

            //排序
            list.Sort(delegate(UIPet a, UIPet b)
            {
                //上阵的人优先
                if (a.owner != null)
                {
                    if (b.owner == null)
                    {
                        return -1;
                    }
                    else
                    {
                        if (a.owner.campPos < b.owner.campPos)
                            return -1;
                        else if (a.owner.campPos > b.owner.campPos)
                            return 1;
                    }
                }
                else
                {
                    if (b.owner != null)
                        return 1;
                }
                //品质高的优先
                if (a.petData.quality > b.petData.quality)
                {
                    return -1;
                }
                else if (a.petData.quality < b.petData.quality)
                {
                    return 1;
                }
                else
                {
                    if (a.level > b.level)
                        return -1;
                    else if (a.level < b.level)
                        return 1;
                    else
                    {
                        if (a.breachlevel > b.breachlevel)
                            return -1;
                        else if (a.breachlevel < b.breachlevel)
                            return 1;
                    }
                }

                //id相同的在一起
                return a.petId.CompareTo(b.petId);
            });

            return list;
        }
        

        /// <summary>
        /// 背包排序
        /// </summary>
        /// <returns></returns>
        public List<UIHero> GetHeroListForBagInOrder()
        {
            List<UIHero> list = new List<UIHero>();
            foreach (var item in instHeros)
            {
                list.Add(item.Value);
            }

            //排序
            list.Sort(delegate (UIHero a, UIHero b)
            {
                //上阵的人优先
                if (a.owner != null)
                {
                    if (b.owner == null)
                    {
                        return -1;
                    }
                    else
                    {
                        if (a.owner.campPos < b.owner.campPos)
                            return -1;
                        else if (a.owner.campPos > b.owner.campPos)
                            return 1;
                    }
                }
                else
                {
                    if (b.owner != null)
                        return 1;
                }

                //助阵优先
                bool a_help = a.Help();
                bool b_help = b.Help();

                if (a_help && !b_help)
                    return -1;
                else if (!a_help && b_help)
                    return 1;
                
                //品质高的优先
                if (a.heroData.quality > b.heroData.quality)
                {
                    return -1;
                }
                else if (a.heroData.quality < b.heroData.quality)
                {
                    return 1;
                }
                else
                {
                    if (a.level > b.level)
                        return -1;
                    else if (a.level < b.level)
                        return 1;
                    else
                    {
                        if (a.breachlevel > b.breachlevel)
                            return -1;
                        else if (a.breachlevel < b.breachlevel)
                            return 1;
                    }
                }

                //id相同的在一起
                return a.heroId.CompareTo(b.heroId);

            });

            return list;
        }

		bool YellowHeroCanSell(string heroID,int yellow)
		{
			if (yellow != (int)QualityValue.YELLOW)
				return true;
			if (heroID.Substring(5,3).Equals ("exp"))
				return true;
			else
				return false;
		}
        /// <summary>
        /// 可出售的宠物
        /// </summary>
        /// <returns></returns>
        public List<UIPet> GetPetListForSellInOrder()
        {
            List<UIPet> list = new List<UIPet>();
            //排序
            list.Sort(delegate(UIPet a, UIPet b)
            {
                //品质高的优先
                if (a.petData.quality > b.petData.quality)
                {
                    return 1;
                }
                else if (a.petData.quality < b.petData.quality)
                {
                    return -1;
                }
                else
                {
                    if (a.level > b.level)
                        return 1;
                    else if (a.level < b.level)
                        return -1;
                    else
                    {
                        if (a.breachlevel > b.breachlevel)
                            return 1;
                        else if (a.breachlevel < b.breachlevel)
                            return -1;
                    }
                }

                //id相同的在一起
                return a.petId.CompareTo(b.petId);

            });

            return list;
        }
        /// <summary>
        /// 可出售的人物
        /// </summary>
        /// <returns></returns>
        public List<UIHero> GetHeroListForSellInOrder()
        {
            List<UIHero> list = new List<UIHero>();
            foreach (var item in instHeros)
            {
				if (item.Value.owner == null && !item.Value.Help() && item.Value.heroData.quality <= (int)QualityValue.YELLOW && 
                    item.Value.heroData.heroClass != (int)HeroClass.Main_Actor_Man && item.Value.heroData.heroClass != (int)HeroClass.Main_Actor_Woman)
				{
					if(YellowHeroCanSell(item.Value.heroId,item.Value.heroData.quality))
						list.Add(item.Value);
				}
            }

            //排序
            list.Sort(delegate (UIHero a, UIHero b)
            {
                //品质高的优先
                if (a.heroData.quality > b.heroData.quality)
                {
                    return 1;
                }
                else if (a.heroData.quality < b.heroData.quality)
                {
                    return -1;
                }
                else
                {
                    if (a.level > b.level)
                        return 1;
                    else if (a.level < b.level)
                        return -1;
                    else
                    {
                        if (a.breachlevel > b.breachlevel)
                            return 1;
                        else if (a.breachlevel < b.breachlevel)
                            return -1;
                    }
                }

                //id相同的在一起
                return a.heroId.CompareTo(b.heroId);

            });

            return list;
        }

        public List<string> GetAdvenceAvailableHeroList(string heroId)
        {
            List<string> heros = new List<string>();
            foreach (var item in instHeros)
            {
                if (item.Value.heroId == heroId)
                {
                    if(item.Value.IsZeroHero() && item.Value.owner == null && !item.Value.Help())
                    {
                        heros.Add(item.Value.guid);
                    }
                }
            }
            return heros;
        }

        /// <summary>
        /// 为饰品强化吃卡list排序
        /// </summary>
        /// <param name="list"></param>
        public void SortTreasureListForStrengthSelect(List<UITreasure> list)
        {
            //按照资质由低到高，强化等级由低到高排序
            list.Sort(delegate (UITreasure a, UITreasure b)
            {
                if(a.treasureData.type == -1)
                {
                    if (b.treasureData.type != -1)
                        return -1;
                }
                else
                {
                    if (b.treasureData.type == -1)
                        return 1;
                }
                if (a.treasureData.quality > b.treasureData.quality)
                {
                    return 1;
                }
                else if (a.treasureData.quality < b.treasureData.quality)
                {
                    return -1;
                }
                else
                {
                    if (a.strengthLevel > b.strengthLevel)
                        return 1;
                    else if (a.strengthLevel < b.strengthLevel)
                        return -1;
                }
                return a.treasureId.CompareTo(b.treasureId);
            });
        }

        /// <summary>
        /// 获取饰品精炼可以吃的卡，上阵的/强化过/精炼过 的不能吃
        /// </summary>
        /// <returns></returns>
        public List<UITreasure> GetTreasureRefineAvailableList(string id)
        {
            List<UITreasure> list = new List<UITreasure>();
            foreach (var item in instTreasures)
            {
                if (item.Value.treasureId == id && item.Value.owner == null && item.Value.strengthLevel == 1 && item.Value.refineLevel == 0)
                {
                    list.Add(item.Value);
                }
            }
            return list;
        }
        /// <summary>
        /// 培养过的宠物，在突破时，不能被吃掉
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public List<UIPet> GetPetBreachAvailableList(string id)
        {
            List<UIPet> list = new List<UIPet>();
            foreach (var item in instPets)
            {
                if (item.Value.petId == id && item.Value.owner == null && item.Value.level == 1 && item.Value.breachlevel == 0 && item.Value.intimacyLevel ==0)
                {
                    list.Add(item.Value);
                }
            }
            return list;
        }

        /// <summary>
        ///  背包排序
        /// </summary>
        /// <param name="list"></param>
        public void SortEquipListForBag(List<UIEquipment> list)
        {
            list.Sort(delegate (UIEquipment a, UIEquipment b)
            {
                //上阵的优先
                if (a.owner != null)
                {
                    if (b.owner == null)
                    {
                        return -1;
                    }
                    else
                    {
                        //if (a.owner.campPos < b.owner.campPos)
                        //    return -1;
                        //else if (a.owner.campPos > b.owner.campPos)
                        //    return 1;
                        if (a.equipData.type < b.equipData.type)
                            return -1;
                        else if (a.equipData.type > b.equipData.type)
                            return 1;
                    }
                }
                else
                {
                    if (b.owner != null)
                        return 1;
                }

                //品质高的优先
                if (a.equipData.quality > b.equipData.quality)
                {
                    return -1;
                }
                else if (a.equipData.quality < b.equipData.quality)
                {
                    return 1;
                }
                else
                {
                    if (a.level > b.level)
                        return -1;
                    else if (a.level < b.level)
                        return 1;
                }

                //id相同的在一起
                return a.equipId.CompareTo(b.equipId);

            });
        }

        /// <summary>
        /// 背包排序
        /// </summary>
        /// <param name="list"></param>
        public void SortTreasureListForBag(List<UITreasure> list)
        {
            list.Sort(delegate (UITreasure a, UITreasure b)
            {
                //上阵的优先
                if (a.owner != null)
                {
                    if (b.owner == null)
                    {
                        return -1;
                    }
                    else
                    {
                        //if (a.owner.campPos < b.owner.campPos)
                        //    return -1;
                        //else if (a.owner.campPos > b.owner.campPos)
                        //    return 1;
                        if (a.treasureData.type < b.treasureData.type)
                            return -1;
                        else if (a.treasureData.type > b.treasureData.type)
                            return 1;
                    }
                }
                else
                {
                    if (b.owner != null)
                        return 1;
                }

                //品质高的优先
                if (a.treasureData.quality > b.treasureData.quality)
                {
                    return -1;
                }
                else if (a.treasureData.quality < b.treasureData.quality)
                {
                    return 1;
                }
                else
                {
                    if (a.strengthLevel > b.strengthLevel)
                        return -1;
                    else if (a.strengthLevel < b.strengthLevel)
                        return 1;
                }

                //id相同的在一起
                return a.treasureId.CompareTo(b.treasureId);

            });
        }

        /// <summary>
        /// 是否有可装备的装备
        /// </summary>
        /// <returns></returns>
        public bool HaveEquipForPos(int pos)
        {
            int type = pos + 1;
            foreach (var item in instEquips)
            {
                if (item.Value.equipData.type == type && item.Value.owner == null)
                    return true;
            }
            return false;
        }

        public bool HaveTreasureForPos(int pos)
        {
            int type = 1;
            if (pos == 4)
                type = 1;
            else if (pos == 5)
                type = 0;

            foreach (var item in instTreasures)
            {
                if (item.Value.treasureData.type == type && item.Value.owner == null)
                    return true;
            }

            return false;
        }

        #endregion

    }
}
